package com.wang.filter;

import com.wang.utils.ConstantsUtil;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author wr
 * @date 2021/10/29 11:30
 */
@WebFilter({"/home.jsp", "/home/*"})
public class LoginFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        //拦截未登录用户直接访问home
        System.out.println("enter login filter");
        String user = (String) req.getSession().getAttribute(ConstantsUtil.Login.USER_KEY);
        if (user != null && !"".equals(user)) {
            chain.doFilter(req, res);
        } else {
            res.sendRedirect("/login.jsp");
        }
    }
}
